<?php 
/**
 * User: Lijun
 * Date: 2017/7/4.
 * Time: 15:46.
 * Comment:文章管理
 */
namespace Admin\Controller;
use Think\Controller;

class ArticleController extends Controller{

	public function index(){
		if($_GET['title']){
			$title = I('title');
			$args['title'] = $title;
			$map['title'] = array("like","%{$title}%");
		}
		$count = M("article") -> where($map) -> count();
		$page = new \Think\Page($count,10,$args);
		$show = $page -> show();
		$articles = array();
		$article = M('article') -> where($map)-> order("id desc") ->limit($page -> firstRow.','.$page -> listRows) -> select();
		foreach ($article as $value) {
			$pid = $value['pid'];
			$classname = M("category") -> where("id = {$pid}") ->getField("catname");
			$tempArr['id'] = $value['id'];
			$tempArr['pid'] = $classname;
			$tempArr['title'] = $value['title'];
			$tempArr['content'] = $value['content'];
			$tempArr['time'] = $value['time'];
			$tempArr['flag'] = $value['flag'];
			array_push($articles, $tempArr);
		}
		$this -> assign('title',$title);
		$this -> assign('articles',$articles);
		$this -> assign('page',$show);
		$this -> display();
	}

	public function addArticle(){
            $category = M("category") -> where(array("pid" => 0)) -> order("sort asc") -> select();
            foreach($category as $k=> $v ) {
                $category[$k]['child'] = M("category") -> where(array("pid" => $v['id'])) -> select();
            }
            $this -> assign("category",$category);
            $this -> display();
	}

	public function doAddArticle(){
            $info['pid'] = I("post.pid");
            $info["title"] = I("post.title");
            $info["source"] = I("post.source");
            $info["content"] = $_POST['content'];
            $info['flag'] = I("post.flag");
            $info['time'] = time();
            if($_FILES["image"]["error"] != 4){
                $upload = new \Think\Upload();
                $upload -> maxSize = 3145728;
                $upload -> exts = array("jpg","jpeg","png","gif");
                $upload -> autoSub = false;
                $upload -> rootPath = "./Public/";
                $upload -> savePath = "uploads/";
                if($image = $upload -> upload()){
                    $info["image"] = "/Public/".$image["image"]["savepath"].$image["image"]["savename"];
                }else{
                    $this -> error("error");
                }
            }
            if(M("article") -> add($info)){
                $this -> success('添加成功',U('index'));
            }else{
                $this -> error('添加失败');
            }
	}

	public function modArticle(){
            $category = M("category") -> where(array("pid" => 0)) -> order("sort asc") -> select();
            foreach($category as $k=> $v ) {
                $category[$k]['child'] = M("category") -> where(array("pid" => $v['id'])) -> select();
            }
            $id = I('id');
            $pids = M("article") -> where("id = {$id}") -> select();
            $pid = $pids[0]['pid'];
            $article = $pids[0];
            $this -> assign("category",$category);
            $this -> assign('pid',$pid);
            $this -> assign('article',$article);
            $this -> display();
	}

	public function doModArticle(){
		$id = $_POST['id'];
		$info['pid'] = I("post.pid");
                $info['title'] = htmlspecialchars($_POST['title']);
		$info['content'] = $_POST['content'];
                if($_FILES['image']['error'] != 4){
                    $upload = new \Think\Upload();
                    $upload -> maxSize = 3145728;
                    $upload -> exts = array("jpg","jpeg","png","gif");
                    $upload -> autoSub = false;
                    $upload -> rootPath = "./Public/";
                    $upload -> savePath = "uploads/";
                    if($image = $upload -> upload()){
                        $info["image"] = "/Public/".$image["image"]["savepath"].$image["image"]["savename"];
                    }else{
                        $this -> error("error");
                    }
                }
		$info['flag'] = $_POST['flag'];
		$info['time'] = time();
		if(M("article") -> where("id = {$id}") -> save($info)){
			$this -> success('修改成功',U('index'));
		}else{
			$this -> error('修改失败');
		}
	}

	public function delArticle(){
		if(IS_GET){
			$id = I('id');
			if(M('article') -> where("id = {$id}") -> delete()){
				$this -> success('删除成功',U('index'));
			}else{
				$this -> error('删除失败');
			}
		}

		if(IS_POST){
			$ids = I('id');
			$id = implode(',', $ids);
			if(M('article') -> where("id in ({$id})") -> delete()){
				$this -> success('删除成功',U('index'));
			}else{
				$this -> error('删除失败');
			}
		}
	}








}

 ?>